﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BattleShipPro
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Session _playerSession;
        private Session _computerSession;
        private int _opponentAttackCount = 0;
        private int _playerAttackCount = 0;
        private int _playerACHitCount = 0;
        private int _oponentACHitCount = 0;
        private int _playerBSHitCount = 0;
        private int _oponentBSHitCount = 0;
        private int _playerDSHitCount = 0;
        private int _oponentDSHitCount = 0;
        private int _playerSBHitCount = 0;
        private int _oponentSBHitCount = 0;
        private int _playerPBHitCount = 0;
        private int _oponentPBHitCount = 0;

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindowLoaded;
        }

        void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            ShowMenu(true);
        }

        private void ShowMenu(bool show)
        {
            menu.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
        }

        private void BtnNewGameClick(object sender, RoutedEventArgs e)
        {
            //hide menu
            ShowMenu(false);

            //create palyer session
            _playerSession = new Session();
            _playerSession.Create();
            _playerSession.Lost += new EventHandler(_playerSession_Lost);
            userBoard.LoadGame(_playerSession);
            
            //create computer session
            opponentZone.LoadGame(_computerSession);
            _computerSession = new Session();
            _computerSession.Create();
            _computerSession.Lost += new EventHandler(_computerSession_Lost);
        }

        void _computerSession_Lost(object sender, EventArgs e)
        {
            MessageBox.Show("Congratulations!! You Won");
        }

        void _playerSession_Lost(object sender, EventArgs e)
        {
            MessageBox.Show("Sorry, You Lost");
        }

        private void BtnExitClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BtnHelpComputerClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Select an attack position on Opponent Zone", "Select");
        }

        private void OpponentAttacked(object sender, LocationEventArgs e)
        {
            if (_computerSession.ReceiveAttack(e.X, e.Y))
            {
                UpdateOpponentStatus(_computerSession.CurrentHitShip);
            }
            txtOpponentAttackCount1.Text = _playerAttackCount++.ToString();


            var random = new Random();
            int x = random.Next(0, Constants.BoardSize);
            int y = random.Next(0, Constants.BoardSize);
            if (_playerSession.ReceiveAttack(x, y))
            {
                userBoard.ReceiveAttack(x, y);
                UpdatePlayerStatus(_playerSession.CurrentHitShip);
            }
            txtPlayerAttackCount.Text = _opponentAttackCount++.ToString();
        }

        private void UpdatePlayerStatus(BattleShip ship)
        {
            switch (ship.Name)
            {
                case Constants.AircraftCarrier:
                    _playerACHitCount++;
                    txtPlayerACStatus.Text = _playerACHitCount==Constants.AircraftCarrierSpaces ? Constants.Sunk : _playerACHitCount.ToString();
                    break;
                case Constants.BattleShip:
                    _playerBSHitCount++;
                    txtPlayerBTStatus.Text = _playerBSHitCount == Constants.BattleShipSpaces ? Constants.Sunk : _playerBSHitCount.ToString();
                    break;
                case Constants.Destroyer:
                    _playerDSHitCount++;
                    txtPlayerDSStatus.Text = _playerDSHitCount == Constants.DestroyerSpaces ? Constants.Sunk : _playerDSHitCount.ToString();
                    break;
                case Constants.Submarine:
                    _playerSBHitCount++;
                    txtPlayerSBStatus.Text = _playerSBHitCount == Constants.SubmarineSpaces ? Constants.Sunk : _playerSBHitCount.ToString();
                    break;
                case Constants.PatrolBoat:
                    _playerPBHitCount++;
                    txtPlayerPBStatus.Text = _playerPBHitCount == Constants.PatrolBoatSpaces ? Constants.Sunk : _playerPBHitCount.ToString();
                    break;
            }
        }

        private void UpdateOpponentStatus(BattleShip ship)
        {
            switch (ship.Name)
            {
                case Constants.AircraftCarrier:
                    _oponentACHitCount++;
                    txtCompACStatus.Text = _oponentACHitCount == Constants.AircraftCarrierSpaces ? Constants.Sunk : _oponentACHitCount.ToString();
                    break;
                case Constants.BattleShip:
                    _oponentBSHitCount++;
                    txtCompBTStatus.Text = _oponentBSHitCount == Constants.BattleShipSpaces ? Constants.Sunk : _oponentBSHitCount.ToString();
                    break;
                case Constants.Destroyer:
                    _oponentDSHitCount++;
                    txtCompDSStatus.Text = _oponentDSHitCount == Constants.DestroyerSpaces ? Constants.Sunk : _oponentDSHitCount.ToString();
                    break;
                case Constants.Submarine:
                    _oponentSBHitCount++;
                    txtCompSBStatus.Text = _oponentSBHitCount == Constants.SubmarineSpaces ? Constants.Sunk : _oponentSBHitCount.ToString();
                    break;
                case Constants.PatrolBoat:
                    _oponentPBHitCount++;
                    txtCompPBStatus.Text = _oponentPBHitCount == Constants.PatrolBoatSpaces ? Constants.Sunk : _oponentPBHitCount.ToString();
                    break;
            }
        }
    }
}
